<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
   <title>控制系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="account" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>
         
        <script type="text/html" id="small_pic">
            <img src="{{d.small_pic}}"   style="width:100%; height:100%; border-radius:50%; ">
        </script>
        <script type="text/html" id="model_id">
            {{# if(d.model_id == 0){ }}
            普通
            {{# } }}
            {{# if(d.model_id == 1){ }}
            连载
            {{# } }}
            {{# if(d.model_id == 2){ }}
            电影
            {{# } }}
            {{# if(d.model_id == 3){ }}
            连续剧
            {{# } }}
            {{# if(d.model_id == 4){ }}
            下载式
            {{# } }}
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edittable">数据表</a>
            <a class="layui-btn layui-btn layui-btn-xs data-count-edit" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">卸载</a>
        </script>
    </div>
</div>

<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','http'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            http  = layui.http,
            apiurl =http.api;
        init();
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            init(data.field);
            return false;
        });

        form.on('switch(status)', function(obj){
            var loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var status = obj.elem.checked===true?1:0;
            http.httpajax("Personal/projectUpData", {'id':id,'status':status},"POST",function(res) {
                layer.close(loading);
                if(res.code === '0000'){
                    layer.msg(res.msg, {time: 1800, icon: 1});
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                } 
            });
        });

        function init(resulti){
            http.verifyToken();
            table.render({
                elem: '#currentTableId',
                url: apiurl+'Personal/projectList',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    // {type: "checkbox"},
                    // {field: 'id', title: 'ID', sort: true},
                    {field: 'name', title: '项目名称'},
                    {field: 'key', title: '项目key'},
                    {field: 'small_pic', title: '图片',toolbar: '#small_pic'},
                    {field: 'status',title: '状态',toolbar: '#status'},
                    {field: 'model_id', title: '内容模型',toolbar: '#model_id'},
                    {field: 'create_time', title: '创建时间'},
                    {title: '操作',minWidth: 200,toolbar: '#currentTableBar', align: "center"}
                ]],
                where: resulti,
                headers: { //通过 request 头传递
                    Authorization:  layui.data(http.token)['token']
                },
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line'
            });
        }

        /**
         * toolbar监听事件
         */
         var ts = new Date().getTime();
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/project/add.html?_'+ts,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } 
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var id = data.id;
            console.error("id",id);
            if (obj.event === 'edit') {
                console.error("id",id);
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/project/add.html?id='+id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的卸载么？将会删除所有数据表！', function (index) {
                    http.httpajax("Personal/projectDel",{'id':id},"POST",function(res) {
                        console.error("res.code",res.code);
                        if(res.code ===  '0000'){
                            layer.msg(res.msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.msg, {time: 1800, icon: 2});
                        }
                    });
                });
            }
            else if (obj.event === 'edittable') {
                http.open(data.name+"---"+data.key+"数据表",'../page/project/projecttablelist.html?id='+id+"$a="+ts);
            }
        });

    });
</script>

</body>
</html>